package com.nonintrusive.client.tabs.answerquestion;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nonintrusive.client.GlobalNonIntrusive;
import com.nonintrusive.client.Question;
import com.nonintrusive.client.QuestionDialog;
import com.nonintrusive.client.send.R;

public class AnswerQuestionAdapter extends ArrayAdapter<Question> {

	private LayoutInflater mInflater;
	private Context mContext;
	private Handler mHandler;
	private int mResource;
	private QuestionDialog qDlg;

	public AnswerQuestionAdapter(Context context, int resource, Handler handler) {
		super(context, resource, GlobalNonIntrusive.answerQuestions);
		mResource = resource;
		mInflater = LayoutInflater.from(context);
		mContext = context;
		mHandler = handler;
		qDlg = new QuestionDialog(context, false);
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public long getItemId(int position) {
		return getItem(position).id;
	}

	@Override
	public View getView(final int _position, View convertView, ViewGroup _parent) {
		final Question question = getItem(_position);

		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(mResource, _parent, false);

			// Creates a ViewHolder and store references to the children views
			// we want to bind data to.
			holder = new ViewHolder();
			holder.message = (TextView) convertView
					.findViewById(R.id.q_message);

			holder.maximize = (ImageView) convertView
					.findViewById(R.id.q_maximize);

			holder.remove = (ImageView) convertView.findViewById(R.id.q_remove);
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.q_layout);

			// The convertView is bound to the holder instance
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.message.setText(question.message);
		if (question.isAnswered) {
			holder.layout.setBackgroundColor(Color.GREEN);
		} else {
			holder.layout.setBackgroundColor(Color.YELLOW);
		}
		holder.remove.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO
				// remove question server_list ("unregister" listeners)
				GlobalNonIntrusive.removeQID(mContext, question, mHandler);
			}
		});

		holder.maximize.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO show a dialog
				// if non-answered Q, can submit answer
				// else simple representation

				holder.maximize.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						qDlg.setQuestion(question);
						qDlg.show();
					}
				});

			}
		});

		return convertView;
	}

	private static class ViewHolder {
		TextView message;
		ImageView maximize;
		ImageView remove;
		LinearLayout layout;
	}
}
